package com.zkingsoft.services.impl;

import com.zkingsoft.services.StudentinfoService;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.DateUtils;
import com.zkingsoft.model.Studentinfo;
import com.zkingsoft.dao.StudentinfoDao;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2017-06-08 17:19
 */
@Service("studentinfoService")
public class  StudentinfoServiceImpl implements    StudentinfoService  {

	
	@Autowired
	private StudentinfoDao studentinfoDao;
	
	
	@Override
	public int add(Studentinfo studentinfo){
		studentinfo.setStuNo(createStuNo());
		return studentinfoDao.insert(studentinfo);
		
	}
   	
    @Override
	public int modify(Studentinfo studentinfo){
	
		return studentinfoDao.update(studentinfo);
	
	}
	
	@Override
	public int remove(List<Long> list){
	
		return studentinfoDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(Long stuId){
	
		return studentinfoDao.deleteById(stuId);
	
	}
	
	@Override
	public int removeByModel(Studentinfo studentinfo){
	
		return studentinfoDao.deleteByModel(studentinfo);
	
	}
	
	
	@Override
	public List<Studentinfo> findInPage(Studentinfo studentinfo,  PaginationVO pageVo){
	
		return studentinfoDao.selectInPage(studentinfo , pageVo);
	
	}
	
	@Override
	public List<Studentinfo> findByModel(Studentinfo studentinfo){
	
		return studentinfoDao.selectByModel(studentinfo);
	
	}
	
	@Override
	public int  findTotal(Studentinfo studentinfo){
	
		return studentinfoDao.selectTotalRecord(studentinfo);
	
	}
	
	@Override
	public Studentinfo  findById(Long stuId){
	
		return studentinfoDao.selectById(stuId);
	
	}
	
	/**
	 * 
	 * @Description:创建学生编号
	 * @author:dingchuan
	 * @return
	 * 返回类型 String
	 * @date 2016年12月16日
	 */
	public String createStuNo(){
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_FORMAT_MM_NO_DD);
		String nowDateTime = sdf.format(new Date());//当前系统时间的年月字符串
		String maxNo = "";
		String checkMaxNo = studentinfoDao.selectMaxNo();
		if(checkMaxNo!=null && !"".equals(checkMaxNo)){
			String checkDateTime = checkMaxNo.substring(1, checkMaxNo.length()-3);
			if(nowDateTime.equals(checkDateTime)){
				Long oldMaxNo = Long.parseLong(checkMaxNo.substring(1,checkMaxNo.length()));
				Long newMaxNo = oldMaxNo + 1;
				maxNo = "D"+ newMaxNo;
			}else{
				//这个月第一条数据
				maxNo = "D"+nowDateTime+"001";//D201612001
			}
		}else{
			//表中第一条数据
			maxNo = "D"+nowDateTime+"001";//D201612001
		}
		return maxNo;
	}

	@Override
	public int adds(List<Studentinfo> studentinfoList) {
		int count =0;
		for(Studentinfo studentinfo : studentinfoList){
			studentinfoDao.insert(studentinfo);
			count++;
		}
		return count;
	}
	
}